package com.dev.web.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dev.bbs.domain.BbsComment;
import com.dev.bbs.domain.BbsUser;
import com.dev.bbs.service.IBbsCommentService;
import com.dev.bbs.service.IBbsMentLikeService;
import com.dev.common.core.controller.BaseController;
import com.dev.common.core.domain.AjaxResult;
import com.dev.framework.util.ShiroUtils;
import com.github.pagehelper.PageInfo;

/**
 * 帖子评论Controller
 *
 * @author dev
 * @date 2019-09-23
 */
@Controller
@RequestMapping("comment")
public class BbsCommentController extends BaseController
{
    @Autowired
    private IBbsCommentService  bbsCommentService;

    @Autowired
    private IBbsMentLikeService mentLikeService;

    /**
    * 查询帖子评论列表
    */
    @SuppressWarnings({"rawtypes", "unchecked"})
    @PostMapping("/list")
    @ResponseBody
    public AjaxResult list(BbsComment bbsComment)
    {
        startPage();
        List<BbsComment> list = bbsCommentService.selectBbsCommentList(bbsComment);
        BbsUser currentUser = ShiroUtils.getBbsUser();
        List<Long> likes=new ArrayList<Long>();
        if (null != currentUser&&list.size()>0)
        {
            List<Long> mentIds = list.stream().map(BbsComment::getMentId).collect(Collectors.toList());
            likes= mentLikeService.getUserLikesIds(currentUser.getUserId(), mentIds);
        }
        AjaxResult result = success().put("rows", list).put("total", new PageInfo(list).getTotal()).put("currentUser",
                currentUser).put("likes", likes);
        return result;
    }
}
